package com.test3;

import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;

public class WechatPlug {

    public static String defult_back_img = "https://tmp.s.weshaketv.com/1234567/lizhao/wechat.png";

    public static String wechatUrl = "https://open.weixin.qq.com/qr/code?username=";

    protected static final String defultPath = "D:/";

    protected static File createPath(String path){
        File file = new File(path);

        if(!file.getParentFile().exists()){
            file.mkdirs();
        }
        return file;
    }

    public static void main(String[] args) {
        File file = createPath(defultPath+"/aa.png");
        sava("gh_a123e8031189",file,"测试");
    }

    /**
     * 生成插件码到固定路劲
     * @param gid 微信原始id
     * @param diskFile 文件放置目录
     * @param vname 村名
     * */
    public static void sava(String gid,File diskFile, String vname){
        getqrCode(gid,diskFile,vname);
    }

    private static void getqrCode(String gid,File diskFile, String vname){

        String path = wechatUrl+ gid;
        try {
            URL uri = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) uri.openConnection();

            // 设置请求方法为HEAD，这样只获取响应头信息而不下载图片内容
            //connection.setRequestMethod("HEAD");

            // 获取Content-Type头信息
            String contentType = connection.getHeaderField("Content-type");

            if (contentType != null && contentType.startsWith("image/")) {
                BufferedImage image = ImageIO.read(uri);
                weChatWrite(image,diskFile,vname);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void weChatWrite(String path, File diskFile, String vname) throws IOException {
        URL url = new URL(path);
        BufferedImage image = ImageIO.read(url);
        weChatWrite(image,diskFile,vname);
    }

    /**
     * 上传的图片写成插件码
     * @param image 原本的code码
     * @throws IOException
     * */
    public static void weChatWrite(BufferedImage image, File diskFile, String vname) throws IOException {

        //背景图
        URL url2 = new URL(defult_back_img);
        BufferedImage image2 = ImageIO.read(url2);
        int createWidth = image2.getWidth();
        int createHeigh = image2.getHeight();
        BufferedImage back = new BufferedImage(createWidth, createHeigh, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = back.createGraphics();
        //绘制白色背景
        //graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, createWidth,createHeigh);

        //消除文字锯齿
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //消除画图锯齿
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //添加插件码
        graphics.drawImage(image, 100, 328, 450, 450, null);//往画布添加图片，参数有位置及图片宽高

        //添加背景
        graphics.drawImage(image2, 0, 0, createWidth, createHeigh, null);//往画布添加图片，参数有位置及图片宽高



        //长按识别文案
        graphics.setPaint(Color.WHITE);
        Font font = new Font("思源黑体 CN Medium",Font.PLAIN,28);
        graphics.setFont(font);
        FontMetrics fontMetrics = graphics.getFontMetrics(font);
        // 文字宽度
        int textWidth = fontMetrics.stringWidth("微信扫二维码");
        // 计算出中心点，并且绘制出文字 x 居中
        graphics.drawString("微信扫二维码", createWidth/2 - textWidth/2, 80);



        graphics.setPaint(Color.WHITE);
        Font font1 = new Font("思源黑体 CN Medium",Font.BOLD,40);
        graphics.setFont(font1);
        FontMetrics fontMetrics1 = graphics.getFontMetrics(font1);
        // 文字宽度
        int textWidth1 = fontMetrics1.stringWidth("关注\""+"新立村新立村新立村"+"\"");
        // 计算出中心点，并且绘制出文字 x 居中
        graphics.drawString("关注\""+"新立村新立村新立村"+"\"", createWidth/2 - textWidth1/2, 147);

        graphics.dispose();
        ImageIO.write(back, "png", diskFile);

    }

    /**
     * @param graphics Graphics2D画布对象
     * @param backWidth 背景宽
     * @param y 文字从y轴什么地方写起
    //	 * @param msg需要写入的文字
     * @param length 按实际长度进行切割 写入
    //	 * @param nextHeigh下一个高度
     * @param font 字体
     * @return 写入了几次
     * */
    private static int fontWrite(Graphics2D graphics,int backWidth,int y,String msg,int length,int nextHeigh,Font font) {
        msg = StringUtils.isBlank(msg)?"":msg;

        graphics.setFont(font);
        FontMetrics fontMetrics = graphics.getFontMetrics(font);


        BigDecimal aBig = new BigDecimal(msg.length());
        BigDecimal bBig = new BigDecimal(length);
        //向上取整
        int num1 = aBig.divide(bBig,BigDecimal.ROUND_UP ).intValue();

        //根据行数决定高度
        y = num1==1?y:y-(num1*10);
        for(int i=0;i<num1;i++) {
            String ms = msg.substring(i*length, (i+1)*length>msg.length()?msg.length():(i+1)*length);
            // 文字宽度
            int textWidth = fontMetrics.stringWidth(ms);
            graphics.drawString(ms, backWidth/2 - textWidth/2, y+i*nextHeigh);
        }
        return num1;
    }


}
